﻿using Max.PH5_2008A.ShoppingCode.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2008A.ShoppingCode.API.Controllers
{
   // [Route("api/[controller]")]
   // [ApiController]
    public class BaseController<T> : ControllerBase where T : class,new()
    {
        IBaseService<T> _service;

        public BaseController(IBaseService<T> service)
        {
            _service = service;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Delete()
        {            
            return Ok();
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual IActionResult Query()
        {
            var list = _service.Query();
            
            return Ok(list);
        }

        /// <summary>
        /// 查询数据带分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("pages")]
        public virtual IActionResult Query(int pageIndex=1,int pageSize=8)
        {
            var list = _service.Query(pageIndex,pageSize,out int total);

            return Ok(new { items = list,total});
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Update()
        {
            return Ok();
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual IActionResult Insert(T t)
        {
           bool result = _service.Insert(t);
            return Ok(result);
        }
    }
}
